package com.yunxin.download.server.api;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.common.result.Result;
import com.yunxin.common.result.ResultCode;
import com.yunxin.download.server.api.request.AddDownloadMethodRequest;
import com.yunxin.download.server.api.request.UpdateDownloadMethodRequest;
import com.yunxin.download.server.service.extenal.ExportConfigDTO;
import com.yunxin.download.server.service.extenal.FileExportConfigDTO;
import com.yunxin.download.server.infra.repository.DownloadMethodConfigDao;
import com.yunxin.download.server.infra.utils.ExportConfigUtils;
import com.yunxin.download.server.infra.utils.TemplateFileUtils;
import com.yunxin.download.server.model.entity.DownloadMethodConfigWithBLOBs;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author zero°
 * @since 2023-06-15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/download/method/config")
public class DownloadMethodConfigController {



    private final DownloadMethodConfigDao methodConfigDao;

    /**
     * 新增下载方法配置
     * @param request request
     * @param httpRequest httpRequest
     * @return  Result<Boolean>
     */
    @PostMapping("/addMethodConfig")
    public Result<Boolean> addMethodConfig(@Validated AddDownloadMethodRequest request, HttpServletRequest httpRequest) {
        MultipartFile file = getFile(httpRequest);
        ExportConfigDTO configDTO = ExportConfigUtils.X.getConfig(file);
        TemplateFileUtils.X.validateJson(configDTO.getConfig(), new TypeReference<List<FileExportConfigDTO>>() {
        });
        DownloadMethodConfigWithBLOBs config = new DownloadMethodConfigWithBLOBs();
        config.setMethodCode(request.getMethodCode());
        config.setMethodName(request.getMethodName());
        String ossKey = UUID.randomUUID().toString();
        TemplateFileUtils.X.createExportTemplate(configDTO.getConfig(), ossKey);
        config.setMethodTemplate(ossKey);
        config.setMethodSql(configDTO.getSql());
        config.setExportConfig(configDTO.getConfig());
        config.setSystemCode(request.getSystemCode());
        config.setLimitType(request.getLimitType());
        config.setCreator(request.getCreator());
        int count = methodConfigDao.insertSelective(config);
        return Result.success(count > 0);
    }

    /**
     * 更新下载方法配置
     * @param request request
     * @param httpRequest httpRequest
     * @return Result<Boolean>
     */
    @PostMapping("/updateMethodConfig")
    public Result<Boolean> updateMethodConfig(@Validated UpdateDownloadMethodRequest request, HttpServletRequest httpRequest) {
        MultipartFile file = getFile(httpRequest);
        ExportConfigDTO configDTO = null;
        if (Objects.nonNull(file)) {
            configDTO = ExportConfigUtils.X.getConfig(file);
            TemplateFileUtils.X.validateJson(configDTO.getConfig(), new TypeReference<List<FileExportConfigDTO>>() {
            });
        }
        DownloadMethodConfigWithBLOBs config = methodConfigDao.selectByPrimaryKey(request.getId());
        if (Objects.isNull(config)) {
            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
        }
        config.setLimitType(request.getLimitType());
        config.setMethodName(request.getMethodName());
        if (Objects.nonNull(configDTO)) {
            String ossKey = UUID.randomUUID().toString();
            TemplateFileUtils.X.createExportTemplate(configDTO.getConfig(), ossKey);
            config.setMethodTemplate(ossKey);
            config.setExportConfig(configDTO.getConfig());
            config.setMethodSql(configDTO.getSql());
        }
        config.setEditor(request.getEditor());
        config.setEditTime(new Date());
        config.setVersion(config.getVersion() + 1);
        int count = methodConfigDao.updateByPrimaryKeySelective(config);
        return Result.success(count > 0);
    }

    /**
     * 获取文件
     * @param httpRequest httpRequest
     * @return MultipartFile
     */
    private MultipartFile getFile(HttpServletRequest httpRequest) {
        MultipartHttpServletRequest mu = (MultipartHttpServletRequest) httpRequest;
        return mu.getFile("file");
    }




}
